import { DictTag } from "@/components/common/DictTag";
import { dealDataCut, dealDataExcept } from "@/utils/common";
import { goodsCodeRend, goodsNameRend } from "@/utils/tableColumns";
import { ElTag } from "element-plus";
import { h } from "vue";
export function useColumns() {
  // 订单表格
  const orderColumns: TableColumnList = [
    {
      label: "备货订单号",
      prop: "purchaseOrderCode",
      minWidth: 120
    },
    {
      label: "供应商名称",
      prop: "supplierName",
      minWidth: 120
    },
    {
      label: "状态",
      prop: "orderStatus",
      minWidth: 100,
      cellRenderer: ({ row }) => getOrderStatus(row.orderStatus)
    },
    {
      label: "备货总金额",
      prop: "orderPrice",
      minWidth: 80,
      formatter: ({ orderPrice }) => dealDataExcept(orderPrice)
    },
    {
      label: "备货商品总数",
      prop: "orderNum",
      minWidth: 100
    },
    {
      label: "到货仓库",
      prop: "depositoryName",
      minWidth: 100
    }
  ];

  // 获取订单状态 primary success danger
  const getOrderStatus = status => {
    if (status == null) {
      return "--";
    }
    // 订单状态 1未到货 2部分发货 3已完成 4已关闭
    let statusMap = {
      1: { text: "未到货", type: "primary" },
      2: { text: "部分发货", type: "primary" },
      3: { text: "已完成", type: "success" },
      4: { text: "已关闭", type: "danger" }
    };
    let obj = statusMap[status];
    if (!obj) {
      return "--";
    }
    return h(
      ElTag,
      {
        type: obj.type
      },
      () => obj.text
    );
  };

  // 商品明细
  const goodsColumns: TableColumnList = [
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 200,
      align: "left",
      fixed: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "goodsCode",
      align: "left",
      minWidth: 205,
      cellRenderer: goodsCodeRend
    },
    {
      label: "商品条码",
      prop: "barCode",
      minWidth: 120,
      formatter: row => row.barCode || "--"
    },
    {
      label: "商品类型",
      prop: "goodsType",
      minWidth: 90,
      cellRenderer: ({ row }) => (
        <DictTag dict-type="PRODUCT_TYPE" dict-value={row.goodsType} />
      )
    },
    {
      label: "备货商品数量",
      prop: "amount",
      minWidth: 120
    },
    {
      label: "已入库商品数量",
      prop: "storeAmount",
      minWidth: 120
    },
    {
      label: "待入库商品数量",
      prop: "goodsAmount",
      minWidth: 120,
      formatter: ({ amount, storeAmount }) =>
        dealDataCut(amount, storeAmount || 0)
    },
    {
      label: "实际出库数量",
      prop: "stockCount",
      minWidth: 150,
      cellRenderer: ({ row }) => (
        <el-input-number
          v-model={row.specNum}
          class="!w-[130px]"
          placeholder="请输入"
          precision={0}
          min={1}
          max={row.goodsAmount || 99999}
        />
      )
    }
  ];

  // 直接出库商品明细
  const zjckGoodsColumns: TableColumnList = [
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 200,
      align: "left",
      fixed: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "goodsCode",
      align: "left",
      minWidth: 205,
      cellRenderer: goodsCodeRend
    },
    {
      label: "商品条码",
      prop: "barCode",
      minWidth: 120,
      formatter: row => row.barCode || "--"
    },
    {
      label: "商品类型",
      prop: "goodsType",
      minWidth: 120,
      cellRenderer: ({ row }) => (
        <DictTag dict-type="PRODUCT_TYPE" dict-value={row.goodsType} />
      )
    },
    {
      label: "供应商名称",
      prop: "supplierName",
      minWidth: 120,
      formatter: row => row.supplierName || "--"
    },
    {
      label: "出库商品数量",
      prop: "stockCount",
      minWidth: 150,
      cellRenderer: ({ row }) => (
        <el-input-number
          v-model={row.specNum}
          class="!w-[130px]"
          placeholder="请输入"
          precision={0}
          min={1}
          max={row.goodsAmount || 99999}
        />
      )
    },
    {
      label: "操作",
      minWidth: 70,
      slot: "operation"
    }
  ];

  return {
    orderColumns,
    goodsColumns,
    zjckGoodsColumns
  };
}
